<?php
ob_start();
session_start();
include "checksession.php";
date_default_timezone_set('Asia/Bangkok');
$id_member = $_SESSION["id_member"];
include("../connect.php");

$strSQL = "SELECT * FROM userlog WHERE id_member = '$id_member' ";
$objQuery = mysql_query($strSQL);
$objResult = mysql_fetch_array($objQuery);
$depart = $objResult["departid"];

$id_emp = $_GET['emp_id'];
$id_per = $_GET['per_id'];
$permission = $_GET['permission'];
//$strSQL1 = "SELECT * FROM employee INNER JOIN depart ON employee.de_id = depart.de_id LEFT JOIN comname ON employee.cn_id= comname.cn_id WHERE employee.emp_id = '$id_emp' ";
$strSQL1 = "SELECT * FROM employee INNER JOIN depart ON employee.de_id = depart.id INNER JOIN mail ON mail.emp_id = employee.emp_id WHERE employee.emp_id = '$id_emp' ";
$objQuery1 = mysql_query($strSQL1);
$objResult1 = mysql_fetch_array($objQuery1);
if ($objResult1['mail_remark'] == "") {
    $objResult1['mail_remark'] = "-";
}

$strSQL3 = "SELECT * FROM comname WHERE emp_id = '$id_emp' ";
$objQuery3 = mysql_query($strSQL3);
$objResult3 = mysql_fetch_array($objQuery3);
?>
<?
include "../connect.php";
$sql = "SELECT SUBSTRING(COLUMN_TYPE,3)
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA='db_itmanager' 
    AND TABLE_NAME='permissionfile'
    AND COLUMN_NAME='per_permission' ";
$result = mysql_query($sql) or die('error getting enum field ' . mysql_error());
$row = mysql_fetch_array($result);
$regex = "/'(.*?)'/";
preg_match_all($regex, $row[0], $enum_array);
$enum_fields = $enum_array[0];
?>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Adinop Co., Ltd. was founded as an ingredients importer and local manufacturers’ distributor in 1986. We also function as an exporter of finished products</title>
        <link rel="stylesheet" href="../css/style.css"/>
        <script language="javascript" src="../js/jquery/jquery-1.7.1.min"></script>
        <script type="text/javascript" src="../fancybox/fancybox/jquery.mousewheel-3.0.4.pack.js"></script>
        <script type="text/javascript" src="../fancybox/fancybox/jquery.fancybox-1.3.4.pack.js"></script>
        <link rel="stylesheet" type="text/css" href="../fancybox/fancybox/jquery.fancybox-1.3.4.css" media="screen" />
        <link rel="stylesheet" href="../fancybox/style.css" />
        <script type="text/javascript" src="autocomplete.js"></script>
        <link rel="stylesheet" href="autocomplete.css"  type="text/css"/>
        <link href="../css/button.css" rel="stylesheet" type="text/css" />

        <script type="text/javascript">
            $(function() {
                var objSet = "#show_arti_topic";
                var objSetText = "ค้นหา รหัส,ชื่อ";
                if ($(objSet).val() == "") {
                    $(objSet).val(objSetText);
                }
                $(objSet).focus(function() {
                    if ($(objSet).val() == objSetText) {
                        $(objSet).val("");
                    }
                }).blur(function() {
                    if ($(objSet).val() == "") {
                        $(objSet).val(objSetText);
                    }
                });
            });
        </script>
        <script type="text/javascript">

            $(document).ready(function() {

                $("#addpermission").fancybox({
                    'width': '70%',
                    'height': '75%',
                    'autoScale': true,
                    'scrolling': 'auto',
                    'transitionIn': 'elastic',
                    'transitionOut': 'none',
                    'type': 'iframe',
                    'overlayColor': '#000',
                    'hideOnOverlayClick': false,
                    onCleanup: function() {
                        return window.location.reload();
                    }
                });

                $('a[id^="edit"]').fancybox({
                    'width': '100%',
                    'height': '100%',
                    'autoScale': true,
                    'scrolling': 'auto',
                    'transitionIn': 'elastic',
                    'transitionOut': 'none',
                    'overlayColor': '#000',
                    'hideOnOverlayClick': false,
                    onClosed: function() {
                        parent.location.reload(true);
                    }
                });

                $('a[id^="delete"]').fancybox({
                    'width': '20%',
                    'height': '20%',
                    onStart: function() {
                        return window.confirm('คุณต้องการลบรายการนี้ไหม?');
                    },
                    onClosed: function() {
                        parent.location.reload(true);
                    }
                });

            });
        </script>
        <script language="javascript">
            function CheckValidate()
            {

                if (eval("document.frmMain.txtper_description.value") == "") {
                    alert("ห้ามเป็นค่าว่าง");
                    eval("document.frmMain.txtper_description.focus();")
                    return false;
                }

                if (eval("document.frmMain.per_permission.value") == "") {
                    alert("ห้ามเป็นค่าว่าง");
                    eval("document.frmMain.per_permission.focus();")
                    return false;
                }

            }

            function CreateSelectOption(ele)
            {
                var objSelect = document.getElementById(ele);
                var Item = new Option("เลือก permission", "");
                objSelect.options[objSelect.length] = Item;
                var Item = new Option("r", "r");
                objSelect.options[objSelect.length] = Item;
                var Item = new Option("w", "w");
                objSelect.options[objSelect.length] = Item;
                var Item = new Option("m", "m");
                objSelect.options[objSelect.length] = Item;
                /*
                 var objSelect = document.getElementById(ele);
                 var Item = new Option("เลือก permission", ""); 
                 objSelect.options[objSelect.length] = Item;
<?
for ($i = 0; $i < 3; $i++) {
    ?>
                     var Item = new Option("<?= $enum_fields[$i]; ?>", "<?= $enum_fields[$i]; ?>"); 
                     objSelect.options[objSelect.length] = Item;
    <?
}
?>
                 */
            }
            /*
             function OpenPopup(intLine)
             {
             window.open('?Line='+intLine,'myPopup','width=420,height=600,toolbar=0, menubar=0,location=0,status=1,scrollbars=1,resizable=1,left=900,top=50');
             }
             */
            function CreateNewRow()
            {
                var intLine = parseInt(document.frmMain.hdnMaxLine.value);
                intLine++;

                var theTable = document.getElementById("tbExp");
                var newRow = theTable.insertRow(theTable.rows.length)
                newRow.id = newRow.uniqueID

                var newCell

                //*** Column No ***//
                newCell = newRow.insertCell(0);
                newCell.id = newCell.uniqueID;
                newCell.setAttribute("className", "css-name");
                newCell.innerHTML = "<center>" + intLine + "</center>";

                //*** Column product_code ***//
                newCell = newRow.insertCell(1);
                newCell.id = newCell.uniqueID;
                newCell.setAttribute("className", "css-name");
                newCell.innerHTML = "<center><INPUT TYPE=\"TEXT\" SIZE=\"70\" NAME=\"txtper_description_" + intLine + "\"  ID=\"product_code_" + intLine + "\" VALUE=\"\" </center>";

                //*** Column 4 ***//
                newCell = newRow.insertCell(2);
                newCell.id = newCell.uniqueID;
                newCell.setAttribute("className", "css-name");
                newCell.innerHTML = "<center><SELECT NAME=\"permission_" + intLine + "\" ID=\"permission_" + intLine + "\"></SELECT></center>";

                //*** Create Option ***//
                CreateSelectOption("permission_" + intLine)

                document.frmMain.hdnMaxLine.value = intLine;
            }

            function RemoveRow()
            {
                intLine = parseInt(document.frmMain.hdnMaxLine.value);
                if (parseInt(intLine) > 0)
                {
                    theTable = document.getElementById("tbExp");
                    theTableBody = theTable.tBodies[0];
                    theTableBody.deleteRow(intLine);
                    intLine--;
                    document.frmMain.hdnMaxLine.value = intLine;
                }
            }
        </script>

    </head>
    <body onload="CreateNewRow();
        CreateNewRow();
        CreateNewRow();
        CreateNewRow();
        CreateNewRow();"> 
        <body class="body">

            <table align="center" width="90%">
                <tr>
                    <td colspan="2" class="header" >
                        <img src="../images/logo.png"> ใบประวัติสิทธิ์การเข้าถึงข้อมูล 
                            <hr/>
                    </td>
                </tr >
            </table>

            <form method="post" name="frmMain" id="frmMain" onsubmit="return CheckValidate();">

                <?
                $strSQL = "SELECT * FROM `permissionfile` WHERE `per_id` = '$id_per' ";
                $objQuery = mysql_query($strSQL) or die("Error Query [" . $strSQL . "]");
                $Num_Rows = mysql_num_rows($objQuery);
                $n = 1;
                ?>

                <div align = "center">
                    <table width="65%"  border="0" align="center" cellpadding="1" cellspacing="1" class="simply">
                        <thead>
                            <tr>
                                <th width="5%" align = "center">ลำดับ</th>
                                <th width="30%">รายละเอียดเซิฟเวอร์</th>
                                <th width="10%">สิทธิ์การเข้าข้อมูล</th>
                            </tr>
                        </thead>
                        <?
                        while ($objResult = mysql_fetch_array($objQuery)) {
                            ?>
                            <tbody>
                                <tr>

                                    <td><div align ="center"><?php echo $n++; ?></div></td>
                                    <td><input type="text" name="txtper_description" id="txtper_description" value="<?= $objResult["per_description"]; ?>" size="80"/></td>
                                    <td><select name="per_permission" id="per_permission">
                                            <option value=""><-- Permission --></option>
    <? if ($permission == 'r') { ?>
                                                <option value="r" selected>r</option>	
                                                <option value="w" >w</option>		
                                                <option value="m" >m</option>
    <? } else if ($permission == 'w') { ?>
                                                <option value="r" >r</option>	
                                                <option value="w" selected>w</option>		
                                                <option value="m" >m</option>
    <? } else if ($permission == 'm') { ?>
                                                <option value="r" >r</option>	
                                                <option value="w" >w</option>		
                                                <option value="m" selected>m</option>
    <? } ?>
                                        </select>
                                    </td>   
                                </tr>
    <?
}
?>			
                        </tbody>
                    </table>
                    <br/>
                    <input type="submit" name="btnSubmit" id="btnSubmit" value="ตกลง " class="button-link">
                </div>
            </form>
        </body>
<?
if (isset($_POST['btnSubmit'])) {
    $txtper_description = $_POST['txtper_description'];
    $txtper_permission = $_POST['per_permission'];

    $sql1 = "update permissionfile set per_description= '$txtper_description' , per_permission= '$txtper_permission' where per_id ='$id_per' ";
    $rsUpdate = mysql_query($sql1);

    if ($rsUpdate) {
        echo "<script language='javascript'>
			   alert('Update Complete');
			   setTimeout(\"parent.$.fancybox.close()\",1000); 
			  </script>";
        exit();
    }
}
?>
</html>